//
//  SEGLogoutViewModel.m
//  SEGViewModel
//
//  Created by 乔杨 on 2020/7/27.
//

#import "SEGLogoutViewModel.h"
#import "SEGRequestManager.h"

@interface SEGLogoutViewModel ()

@property (nonatomic, strong) SEGRequestManager *logoutManager;

@end

@implementation SEGLogoutViewModel

- (instancetype)init {
    self = [super init];
    if (self) {
        /// 刷新图片验证码
        [self seg_createRefreshImgSignal];
        /// 获取短信验证码
        [self seg_createCaptchasSignal];
        /// 获取语音验证码
        [self seg_createSoundCodeSignal];
        /// 确定按钮
        [self seg_createSureBtnSignal];
    }
    return self;
}

- (void)seg_createRefreshImgSignal {
    @weakify(self);
    self.refreshImgSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        self.checkImgStr = @"";
        [self.logoutManager requestSecurityRefreshCheckCodeWithFinished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                if ([self.logoutManager.seg_segtelcaptchasbyregistermodel.imgCode length] > 0) {
                    NSData *decodedImageData = [[NSData alloc] initWithBase64EncodedString:self.logoutManager.seg_segtelcaptchasbyregistermodel.imgCode options:NSDataBase64DecodingIgnoreUnknownCharacters];
                    UIImage *decodedImage = [UIImage imageWithData:decodedImageData];
                    [subscriber sendNext:decodedImage];
                    [subscriber sendCompleted];
                } else {
                    [subscriber sendNext:nil];
                    [subscriber sendCompleted];
                }
            } else {
                NSError *error = [NSError errorWithDomain:NSCocoaErrorDomain code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];

        return nil;
    }];
}
- (void)seg_createCaptchasSignal {
    @weakify(self);
    self.captchasSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        NSError *error = [self seg_checkCondition];
        if (error) {
            [subscriber sendError:error];
            return nil;
        }
        [self.logoutManager logout_requestGetCaptchasByTel:kAccountMobile imgCode:self.checkImgStr randomToken:self.logoutManager.seg_segtelcaptchasbyregistermodel.randomToken Finished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                [subscriber sendNext:@""];
                [subscriber sendCompleted];
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
 
        return nil;
    }];
}
- (void)seg_createSoundCodeSignal {
    @weakify(self);
    self.soundCodeSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        NSError *error = [self seg_checkCondition];
        if (error) {
            [subscriber sendError:error];
            return nil;
        }
        [self.logoutManager requestGetSoundCodeByTel:kAccountMobile imgCode:self.checkImgStr randomToken:self.logoutManager.seg_segtelcaptchasbyregistermodel.randomToken Type:@"forgetPwd" Finished:^(BOOL isSuccessed,id result,NSString* codeType,NSString* errorMessage) {
            if (isSuccessed) {
                [subscriber sendNext:errorMessage];
                [subscriber sendCompleted];
            } else {
                NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
        
        return nil;
    }];
}
- (void)seg_createSureBtnSignal {
    @weakify(self);
    self.sureBtnSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        [self.logoutManager requestLogoutAccountWithTel:kAccountMobile smsCode:self.smsCodeStr businessCode:@"register" finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
            if (isSuccessed) {
                [subscriber sendNext:errorMessage];
                [subscriber sendCompleted];
            } else {
                if (codeType.integerValue == -4001) { // 短信验证码错误
                    self.smsCodeStr = @"";
                }
                NSError *error = [NSError errorWithDomain:@"" code:1 userInfo:@{NSLocalizedFailureReasonErrorKey:errorMessage}];
                [subscriber sendError:error];
            }
        }];
        
        return nil;
    }];
}

// 检测条件
- (NSError *)seg_checkCondition {
    if (self.checkImgStr.length == 0) {
        return [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedFailureReasonErrorKey:@"请输入图形验证码"}];;
    }
    return nil;
}

#pragma mark - Getter
- (SEGRequestManager *)logoutManager {
    if (!_logoutManager) _logoutManager = [[SEGRequestManager alloc] init];
    return _logoutManager;
}

@end
